package com.ttai.presenter.activity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.ttai.model.dao.DBHelper;
import com.ttai.model.net.ResponseInfo;
import com.ttai.model.net.TipAcountBean;
import com.ttai.presenter.base.BasePresenter;
import com.ttai.ui.activity.AddAcount;
import com.ttai.ui.activity.EnterPage;
import com.ttai.ui.activity.Register;
import com.ttai.ui.activity.SetMiyao;
import com.ttai.ui.activity.SetMiyaoAgain;

import retrofit2.Call;

/**
 * 只进行数据库操作
 */
public class AddAcountPresenter extends BasePresenter{
    AddAcount addAcount;
    private String phonenumber;

    private String value;

    public AddAcountPresenter(AddAcount addAcount) {
        this.addAcount = addAcount;
    }

//    public void dealDB() {
////        if (HomePresrenter.myPhone==null || HomePresrenter.myPhone.isEmpty()){
////            if(Register.phonenumber==null || Register.phonenumber.isEmpty()){
////                phonenumber = EnterPage.enterphonenumber;
////            }else{
////                phonenumber =Register.phonenumber;
////            }
////        }else {
////            phonenumber =HomePresrenter.myPhone;
////        }
////        DBHelper dbHelper = DBHelper.getInstance(addAcount);
//        if((addAcount.acountName==null ||addAcount.acountName.isEmpty())||(addAcount.acountId==null || addAcount.acountId.isEmpty() ) ||(addAcount.acountPassword==null || addAcount.acountPassword.isEmpty()) ){
//            Toast.makeText(addAcount,"加密失败,名称或ID或密码都不能为空",Toast.LENGTH_SHORT).show();
//        }else {
////            SQLiteDatabase sqLiteDatabase=dbHelper.getWritableDatabase();
////            Cursor cursor=sqLiteDatabase.query("tipacount", new String[]{"tip"}, "phone= '"+phonenumber+"'", null,null, null, null);
////            while (cursor.moveToNext()){
////                value =cursor.getString(0);
////            }
////            cursor.close();
//            Log.i("addacountpresenter", "dealDB: valure"+value);
//
//            Intent intent1=new Intent();
//            intent1.putExtra("acountId",addAcount.acountId);
//            intent1.putExtra("value",value);
//            intent1.putExtra("acountPassword",addAcount.acountPassword);
//
//            if (value ==null || value.isEmpty()){
//                intent1.setClass(addAcount,SetMiyao.class);
//                addAcount.startActivity(intent1);
//            }else {
//                intent1.setClass(addAcount,SetMiyaoAgain.class);
//                addAcount.startActivity(intent1);
//            }
//        }
//    }

    @Override
    protected void showError(String message) {
        Toast.makeText(addAcount,message,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void parseJson(JsonObject json) {
        Gson gson = new Gson();
        TipAcountBean tipAcountBean = gson.fromJson(json, TipAcountBean.class);
        String phone = tipAcountBean.getTaiAccount();
        value = tipAcountBean.getTip();
        Log.i("1235", "parseJson: "+phone+"tip:"+value);

        if((addAcount.acountName==null ||addAcount.acountName.isEmpty())||(addAcount.acountId==null || addAcount.acountId.isEmpty() ) ||(addAcount.acountPassword==null || addAcount.acountPassword.isEmpty()) ){
            Toast.makeText(addAcount,"加密失败,名称或ID或密码都不能为空",Toast.LENGTH_SHORT).show();
        }else {
//            SQLiteDatabase sqLiteDatabase=dbHelper.getWritableDatabase();
//            Cursor cursor=sqLiteDatabase.query("tipacount", new String[]{"tip"}, "phone= '"+phonenumber+"'", null,null, null, null);
//            while (cursor.moveToNext()){
//                value =cursor.getString(0);
//            }
//            cursor.close();
            Log.i("addacountpresenter", "dealDB: valure"+value);

            Intent intent1=new Intent();
            intent1.putExtra("acountId",addAcount.acountId);
            intent1.putExtra("value",value);
            intent1.putExtra("acountPassword",addAcount.acountPassword);

            if (value ==null || value.isEmpty()){
                intent1.setClass(addAcount,SetMiyao.class);
                addAcount.startActivity(intent1);
            }else {
                intent1.setClass(addAcount,SetMiyaoAgain.class);
                addAcount.startActivity(intent1);
            }
        }

    }

    public void storeTipAcount(String token){
        Call<ResponseInfo> tipAcountInfo = responseInfoApi.restoreTipAcount(token);
        tipAcountInfo.enqueue(new CallBackAdapter());
    }

}
